package com.kingjw.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @Package: com.kingjw.kafka.interceptor
 * @ClassName: CounterInterceptor
 * @Author: 王坚伟
 * @CreateTime: 2021/10/27 11:39
 * @Description:
 */
public class CounterInterceptor implements ProducerInterceptor<String,String> {

    private int errorCounter = 0;
    private int successCounter = 0;

    public void configure(Map<String, ?> configs) {

    }

    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        return record;
    }

    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        if (exception == null){
            successCounter++;
        }else {
            errorCounter++;
        }

    }

    public void close() {
        System.out.println("Successful sent: "+successCounter);
        System.out.println("Failed sent: "+ errorCounter);

    }
}
